import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant';
// import Home from '../views/Home.vue'
Vue.use(Toast);
// 解决路由版本过高所引起的错误
const originalReplace = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalReplace.call(this, location).catch(err => err)
}
Vue.use(VueRouter)
const Home = () => import('../views/Home.vue')
const About = () => import('../views/About.vue')
const Order = () => import('../views/Order.vue')
const My = () => import('../views/My.vue')

const routes = [
    { path: '/', redirect: '/home' },
	{
		path: '/home',
		component: Home,

	},
	{
		path: '/about',
		component: About,
	    meta:{
		   auth:true
	   }
	},
	{
		path: '/order',
		component: Order,
		meta:{
		   auth:true
		}
	},
	{
		path: '/my',
		component: My
	},
	{
		path: '/details/:id',
		name:'details',
		component: () => import('../views/Details.vue')
	},
	{
		path: '/queren/:conter',
		name: 'queren',
		component: () => import('../views/Queren.vue')
	},
	{
		path: '/payment',
		component: () => import('../views/Payment.vue')
	},
	{
		path: '/address/',
		component: () => import( '../views/Address.vue'),
		meta:{
		   auth:true
		}
	},
	{
		path: '/xiugaidizhi/:id',
		name:'xiugaidizhi',
		component: () => import('../views/Xiugaidizhi.vue')
	},
	{
		path: '/login',
		component: () => import('../views/Login.vue')
	},
	{
		path: '/register',
		component: () => import('../views/Register.vue')
	},
	{
		path: '/historys',
		component: () => import('../views/Historys.vue'),
		meta:{
		   auth:true
		}
	},
	{
		path: '/favorites',
		component: () => import('../views/Favorites.vue'),
		meta:{
		   auth:true
		}
	},
	{
		path: '/modifymy',
		component: () => import('../views/ModifyMy.vue')
	},
	{
		path: '/comment/:id',
		name:'comment',
		component: () => import('../views/Comment.vue')
	},
	{
		path: '/orderdetails/:id',
		name:'orderdetails',
		component: () => import('../views/OrderDetails.vue')
	}
	
]
const router = new VueRouter({
	// mode:'history', //去掉路由的#号
	routes,
	// scrollBehavior (to, from, savedPosition) {
	// 	return { x: 0, y: 0 }
	// }
})

  // 全局路由守卫
  router.beforeEach((to,from,next)=>{
	   if(to.meta.auth && !localStorage.getItem('globalData')){
		   next({
			   path:'/my'
		   })
		  Toast('请先登录');
	   }else{
		   next()
	   }
  })

export default router